import React from "react";
import PubSub from 'pubsub-js';
import "antd/dist/antd.css";
import '../../assets/center/MiddleHeader.css'
import MiddleHeader from "./MiddleHeader";
import MiddleContent from "./MiddleContent";
import {message, Modal, Spin} from "antd";
import {asyncContentPost,
    asyncGetContent
} from "../../api/middle/centerContent";
import {defaultSort, timeSort, nameSort} from "../../assets/center/SortMethods";

export default class Middle extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            FileInfos: [],
            isInvalid: true,
            isFetching: false,
            directory: [],
            search: null,
            Type: 'all',
            sortMethod: defaultSort
        };
    };
    refreshWhenNeeded = async ()=>{
        if(this.state.isInvalid&&!this.state.isFetching) {
            try{
                let optional = {};
                if(this.state.search)
                    optional = {search: this.state.search};
                else if (this.state.Type){
                    switch (this.state.Type) {
                        case 'all': break;
                        case 'favorite': optional = {is_starred: true}; break;
                        default: optional = {file_type: this.state.Type};
                    }
                }

                const [temp] =
                    // [...await asyncGetContent({
                    //     cookie: '',
                    //     directory: '/' + this.state.directory.join('/'),
                    //     ...{optional}
                    // })];
                    //测试用例
                    [[
                        {name: 'fdasfdsafsadfsa.txt', file_id: '1235', is_directory: false, is_starred: false, file_type:'TEXT', created_at: new Date()},
                        {name: '1.jpg', file_id: '1535',is_directory: false, is_starred: true, file_type: 'PHOTO', created_at: new Date()},
                        {name: '2.pdf', file_id: '16635',is_directory: false, is_starred: false, file_type: 'PDF', created_at: new Date()},
                        {name: '3.ppt', file_id: '124345',is_directory: false, is_starred: true, file_type: 'PPT', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'a folder', file_id: '153556',is_directory: true, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: '一个文件', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                        {name: 'txt', file_id: '153545',is_directory: false, is_starred: false, file_type: 'UNKNOWN', created_at: new Date()},
                    ],false];
                this.setState({
                    search: null,
                    Type: null,
                    isInvalid: false,
                    isFetching: false,
                    FileInfos: temp.sort(this.state.sortMethod)});
            }catch (e) {
                Modal.error({
                    title: '文件列表获取失败',
                    content: e
                });
            }
        }
    }

    onSearch = search => {
        this.setState({search: search, isInvalid: true})
    }
    onSort = e =>{
        switch (e.target.value) {
            case 2: this.setState({isInvalid: true, sortMethod: nameSort});     break;
            case 3: this.setState({isInvalid: true, sortMethod: timeSort});     break;
            case 1: default: this.setState({isInvalid: true, sortMethod: defaultSort});     break;
        }
    }
    onRefresh = ()=>{
        this.setState({isInvalid: true});
    }
    onBack = (directory)=>{
        console.log(directory);
        this.setState({directory, isInvalid: true});
    }

    onEnterFolder = folderName =>{
        this.setState({
            directory: [...this.state.directory,folderName],
            isInvalid: true
        });
    }
    //为函数加一个刷新回调
    asyncRefreshCall = (middle)=>
        async (e)=>{
            this.setState({isInvalid: true, isFetching: true});
            middle(e);
            this.setState({isFetching: false});
        }
    //获取期间禁止用户操作
    asyncDisableCall = (middle)=>
        async (e)=>{
            this.setState({isFetching: true});
            middle(e);
            this.setState({isFetching: false});
        }
    onCreatFolder = this.asyncRefreshCall(async ()=>{
        let name = '新建文件夹', flag = false, counter = 0;
        for (const FileInfo of this.state.FileInfos){
            if (FileInfo.name===name){
                flag = true;
                break;
            }
        }
        while (flag) {
            counter++;
            name = `新建文件夹(${counter})`;
            flag = false;
            for (const FileInfo of this.state.FileInfos){
                if (FileInfo.name===name){
                    flag = true;
                    break;
                }
            }
        }
        console.log(this.state.address);
        const [Message] = await asyncContentPost({
            directory: this.state.directory.join('/') + '/' + name
        });
        if(Message.succeed) {
            console.log(`文件夹创建成功`);
        }else {
            message.error(`文件夹创建失败：${Message.msg}`);
        }
    })
    componentDidMount() {
        this.token = PubSub.subscribe('File', (_, stateObj) => {
            this.setState(stateObj)
        });
        this.refreshWhenNeeded();
    }
    componentDidUpdate(prevProps, prevState, snapshot) {
        this.refreshWhenNeeded();
    }
    componentWillUnmount() {
        PubSub.unsubscribe(this.token);
    }
    render() {
        return (
            <Spin spinning={this.state.isInvalid||this.state.isFetching} size='large' tip='Loading...'>
                <div className='center'>
                    <MiddleHeader
                        directory={this.state.directory}
                        onSort={this.onSort}
                        onSearch={this.onSearch}
                        onBack={this.onBack}
                        onCreatFolder={this.onCreatFolder}
                        onRefresh={this.onRefresh}/>
                    <MiddleContent
                        FileInfos={this.state.FileInfos}
                        onEnterFolder={this.onEnterFolder}
                        onRefresh={this.onRefresh}
                        onCreatFolder={this.onCreatFolder}
                        asyncRefreshCall={this.asyncRefreshCall}
                        asyncDisableCall={this.asyncDisableCall}
                    />
                </div>
            </Spin>
        );
    }
}